;;; firemacs-utils.el --- 

;; Copyright 2006 Lukas Loehrer
;;
;; Author: loehrerl [at] gmx [dot] net
;; Version: $Id: firemacs-utils.el,v 0.0 2006/10/18 16:31:05 luke Exp $
;; Keywords: 
;; X-URL: not distributed yet

;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 2, or (at your option)
;; any later version.
;;
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with this program; if not, write to the Free Software
;; Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

;;; Commentary:

;; 

;; Put this file into your load-path and the following into your ~/.emacs:
;;   (require 'firemacs-utils)

;;; Code:

(provide 'firemacs-utils)
(eval-when-compile
  (require 'cl))


(defsubst ensure-major-mode (mode)
  "Make sure current major mode is MODE. Error otherwise."
  (unless (eq major-mode mode)
	(error "Not in %s" mode)))

(defun fm-list-delete-nth (n list)
(pop (nthcdr n list))
list)


(defmacro fm-alist-set (key value alist)
  "In ALIST, set value associated with KEY to VALUE.

Uses eq to compare keys."
  (let ((pair (gensym "alist-set")))
  `(let ((,pair (assq ,key ,alist)))
	(if ,pair
		(setcdr ,pair ,value)
	  (push (cons ,key ,value) ,alist)))))


(defun fm-insert-line-with-props (text props)
  (let ((start (point)))
    (insert-and-inherit text)
    (let ((end (point)))
      (insert-and-inherit "\n")
      (add-text-properties start end props))))

;;{{{Form helpers

(defun firemacs-ui-input-line (prompt initial-value)
  (read-string prompt initial-value))

(defun firemacs-utils-rename-uniquely (choices)
  (let (seen)
	(loop for entry being the elements of  choices collect
		   (let ((cell (assoc entry seen)))
			 (if cell
				 (prog1
					 (format "%s<%d>" entry (cdr cell))
				   (incf (cdr cell)))
			   (progn 
				 (push (cons entry 1) seen)
				 entry))))))
  
(defun firemacs-ui-select (prompt choices current)
  (let* ((unique-choices (firemacs-utils-rename-uniquely choices))
		 (choices-alist (loop for e being the elements of unique-choices for i from 0 collect (cons e i)))
		 (initial-value (car-safe (rassoc current choices-alist)))
		 (my-hist (copy-seq unique-choices))
		 (default current)
		 (choice (completing-read prompt choices-alist nil t initial-value (cons 'my-hist (1+ current)) default)))
	(cdr-safe (assoc choice choices-alist))))
		   
		   

;;  
;; (firemacs-util-select "Dial a pirate: " (list "John" "Steve" "Linux" "Guybrush"))
;; (firemacs-util-select "Dial a pirate: " '(("John" . 0) ("Steve" . 1)  ("Linux" . 2) ("Guybrush" . 3) ("John" . 4)))
;; (setq pirate-history nil)
; (copy-seq pirate-history)
; (setq pirate-list (list "John" "Steve" "Linux" "Guybrush"))



;;}}}

;;{{{Property helpers

(defun add-to-text-property (pos prop item &optional prepend object)
  (let ((items (get-text-property pos prop object)))
	(cond
	 (prepend
	  (setq items (cons item items)))
	 (t
	  (push item items)))
	(put-text-property pos (+ pos 1) prop items object)))


;;}}}

;;{{{  emacs local variables

;;; local variables:
;;; folded-file: t
;;; byte-compile-dynamic: t
;;; end:

;;}}}

;;; firemacs-utils.el ends here
